//
//  GaugeResultViewController.m
//  SeaMelonSeeds_Counselor
//
//  Created by 杨笑 on 2017/9/13.
//  Copyright © 2017年 杨笑. All rights reserved.
//

#import "GaugeResultViewController.h"

@implementation GaugeResultParam

@end


@interface GaugeResultViewController ()

@end

@implementation GaugeResultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"测量结果";
    
}


-(WKWebViewConfiguration *)getWebViewConfiguration{
    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
    WKUserContentController *controller = [[WKUserContentController alloc] init];
    
    [controller addScriptMessageHandler:self name:@"getToken"];
    [controller addScriptMessageHandler:self name:@"isTokenExpire"];
    
    NSDictionary *dict  = @{@"token":[Config Token],@"gauge_test_log_id":@(_param.gaugeId),@"user_id":@(_param.userId),@"serverId":@(_param.serverId),@"type":@(_param.type)};
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:nil];
    
    NSString *str = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    str = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];  //去除掉首尾的空白字符和换行字符
    str =  [str stringByReplacingOccurrencesOfString:@"\n" withString:@""];

    WKUserScript *tokenScript = [[WKUserScript alloc] initWithSource:[NSString stringWithFormat:@"function getToken() {return '%@'}",str] injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES];
    [controller addUserScript:tokenScript];
    
    
    WKUserScript *isTokenExpire = [[WKUserScript alloc] initWithSource:@"function isTokenExpire(token){ window.webkit.messageHandlers.isTokenExpire.postMessage(token);}" injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES];
    [controller addUserScript:isTokenExpire];
    
    configuration.userContentController = controller;
    return configuration;
}

-(void)setParam:(GaugeResultParam *)param{
    _param = param;
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@"http://web.haiguazi.cainiaowx.com/m/gaugeResult.html"]];
    request.timeoutInterval=30.0;//设置请求超时为30秒
    [self.webView loadRequest:request];
}

#pragma mark- JavaScript调用
-(void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
    NSLog(@"%@",message.name);
    if ([message.name isEqualToString:@"isTokenExpire"]){
    //token过期
        NSString *token = message.body;
        if ( [token length] > 10) {
            NSLog(@"详情页更新token\n新token：%@ \n旧token：%@",token,[Config Token]);
            [Config SetToken:token];
        }
    }else if ([message.name isEqualToString:@"getToken"]){
        NSLog(@"getToken().............")
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
